/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.Seckill;
import com.boxin.ims.modules.wechat.service.SeckillService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 微信秒杀Controller
 * @author Jakemanse
 * @version 2013-09-13
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/seckill")
public class SeckillController extends BaseController {

	@Autowired
	private SeckillService seckillService;
	
	@ModelAttribute
	public Seckill get(@RequestParam(required=false) Long id) {
		if (id != null){
			return seckillService.get(id);
		}else{
			return new Seckill();
		}
	}
	
	@RequiresPermissions("wechat:seckill:view")
	@RequestMapping(value = {"list", ""})
	public String list(Seckill seckill, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			seckill.setUser(user);
		}
        Page<Seckill> page = seckillService.find(new Page<Seckill>(request, response), seckill); 
        model.addAttribute("page", page);
		return "modules/wechat/seckillList";
	}

	@RequiresPermissions("wechat:seckill:view")
	@RequestMapping(value = "form")
	public String form(Seckill seckill, Model model) {
		
		if(seckill.getUser() == null){
			seckill.setUser(UserUtils.getUser());
		}
		
		model.addAttribute("seckill", seckill);
		return "modules/wechat/seckillForm";
	}

	@RequiresPermissions("wechat:seckill:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("logofile") MultipartFile logofile,@RequestParam("picfile") MultipartFile picfile,Seckill seckill, Model model,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, seckill)){
			return form(seckill, model);
		}
		
		
		if(logofile != null && logofile.getOriginalFilename().length()>4){
			String seckillPath = WechatUtils.getWechatSeckillResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(seckillPath);
			String sname = logofile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_seckill_"+System.currentTimeMillis()+fileType;
			File file = new File(seckillPath+fname);
			System.out.println("保存文件:"+seckillPath+fname);
			try {
				logofile.transferTo(file);
				seckill.setLogo(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+seckillPath+fname);
				e.printStackTrace();
			}
			
		}
		
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String seckillPath = WechatUtils.getWechatSeckillResourceSavePath(request) + File.separator;
			FileUtils.createDirectory(seckillPath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_seckill_"+System.currentTimeMillis()+fileType;
			File file = new File(seckillPath+fname);
			System.out.println("保存文件:"+seckillPath+fname);
			try {
				picfile.transferTo(file);
				seckill.setPicPath(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+seckillPath+fname);
				e.printStackTrace();
			}
			
		}
		
		seckillService.save(seckill);
		addMessage(redirectAttributes, "保存微信秒杀'" + seckill.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/seckill/?repage";
	}
	
	@RequiresPermissions("wechat:seckill:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		seckillService.delete(id);
		addMessage(redirectAttributes, "删除微信秒杀成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/seckill/?repage";
	}

}
